package manage

import (
	"encoding/json"
	"github.com/jinzhu/copier"

	"github.com/ouqiang/gocron/internal/models"
	"github.com/ouqiang/gocron/internal/modules/logger"
	"github.com/ouqiang/gocron/internal/modules/utils"
	"gopkg.in/macaron.v1"
)

type WebhookForm struct {
	Url      string `json:"url"`
	Template string `json:"template"`
}

func Slack(ctx *macaron.Context) string {
	settingModel := new(models.Setting)
	slack, err := settingModel.Slack()
	jsonResp := utils.JsonResponse{}
	if err != nil {
		logger.Error(err)
		return jsonResp.Success(utils.SuccessContent, nil)

	}

	return jsonResp.Success(utils.SuccessContent, slack)
}

func UpdateSlack(ctx *macaron.Context) string {
	url := ctx.QueryTrim("url")
	template := ctx.QueryTrim("template")
	settingModel := new(models.Setting)
	err := settingModel.UpdateSlack(url, template)

	return utils.JsonResponseByErr(err)
}

func CreateSlackChannel(ctx *macaron.Context) string {
	channel := ctx.QueryTrim("channel")
	settingModel := new(models.Setting)
	if settingModel.IsChannelExist(channel) {
		jsonResp := utils.JsonResponse{}

		return jsonResp.CommonFailure("Channel已存在")
	}
	_, err := settingModel.CreateChannel(channel)

	return utils.JsonResponseByErr(err)
}

func RemoveSlackChannel(ctx *macaron.Context) string {
	id := ctx.ParamsInt(":id")
	settingModel := new(models.Setting)
	_, err := settingModel.RemoveChannel(id)

	return utils.JsonResponseByErr(err)
}

// endregion

// region 邮件
func Mail(ctx *macaron.Context) string {
	settingModel := new(models.Setting)
	mail, err := settingModel.Mail()
	jsonResp := utils.JsonResponse{}
	if err != nil {
		logger.Error(err)
		return jsonResp.Success(utils.SuccessContent, nil)
	}

	return jsonResp.Success("", mail)
}

type MailServerForm struct {
	Host     string `json:"host" binding:"Required;MaxSize(100)"`
	Port     int    `json:"port" binding:"Required;Range(1-65535)"`
	User     string `json:"user" binding:"Required;MaxSize(64);Email"`
	Password string `json:"password" binding:"Required;MaxSize(64)"`
	Template string `json:"template"`
}
type MailForm struct {
	Host     string `json:"host"`
	Port     int    `json:"port"`
	User     string `json:"user"`
	Password string `json:"password"`
}

func UpdateMail(ctx *macaron.Context, form MailServerForm) string {
	settingModel := new(models.Setting)
	template := form.Template
	var MailForm MailForm
	err := copier.Copy(&MailForm, &form)
	if err != nil {
		logger.Error(err)
		jsonResp := utils.JsonResponse{}
		return jsonResp.CommonFailure("修改失败")
	}
	jsonByte, _ := json.Marshal(MailForm)
	err = settingModel.UpdateMail(string(jsonByte), template)
	return utils.JsonResponseByErr(err)
}

type MailUserForm struct {
	Username string `json:"username"`
	Email    string `json:"email"`
}

func CreateMailUser(ctx *macaron.Context, mailUser MailUserForm) string {
	settingModel := new(models.Setting)
	if mailUser.Username == "" || mailUser.Email == "" {
		jsonResp := utils.JsonResponse{}

		return jsonResp.CommonFailure("用户名、邮箱均不能为空")
	}
	_, err := settingModel.CreateMailUser(mailUser.Username, mailUser.Email)

	return utils.JsonResponseByErr(err)
}

func RemoveMailUser(ctx *macaron.Context) string {
	id := ctx.ParamsInt(":id")
	settingModel := new(models.Setting)
	_, err := settingModel.RemoveMailUser(id)

	return utils.JsonResponseByErr(err)
}

func WebHook(ctx *macaron.Context) string {
	settingModel := new(models.Setting)
	webHook, err := settingModel.Webhook()
	jsonResp := utils.JsonResponse{}
	if err != nil {
		logger.Error(err)
		return jsonResp.Success(utils.SuccessContent, nil)
	}

	return jsonResp.Success("", webHook)
}

func UpdateWebHook(ctx *macaron.Context, form WebhookForm) string {
	settingModel := new(models.Setting)
	err := settingModel.UpdateWebHook(form.Url, form.Template)

	return utils.JsonResponseByErr(err)
}

// endregion
